<?php
/**
 * Load a template.
 * @param $name Template name without path and extension.
 */
function template_load($name) {
  global $tbs, $out;
  if (!isset($tbs)) {
    include('tbs/tbs_class_php5.php');
    $tbs = new clsTinyButStrong();
    // TODO: Set gzip compression on
  }
  if ($name) {
    $tbs->LoadTemplate("html/$name.html");
    $tbs->Source = str_replace('../html/', 'html/', $tbs->Source);
  }
  return $tbs;
}


$env_magic_quotes_gpc=false;


function _rnd($min,$max) {
    return mt_rand($min,$max);
}

if (!function_exists('v')){
  function v( $str )
  {
      return isset( $_REQUEST[$str] ) ? $_REQUEST[$str] : false;
  }
}

function used_time($pagestartime){
  $pageendtime = microtime();
  $starttime = explode(" ",$pagestartime); 
  $endtime = explode(" ",$pageendtime); 
  $totaltime = $endtime[0]-$starttime[0]+$endtime[1]-$starttime[1]; 
  $timecost = sprintf("%s",$totaltime); 
  // echo "页面运行时间: $timecost 秒"; 
  // echo "页面运行时间: ". used_time() ."秒"; 
  return $timecost;
}

function getArrayValueByPos($arr,$n){
  $i=0;
  if(!is_array($arr)) return false;
  // $debugMode=1;
  // if($debugMode) {
 //        echo 'DEBUG: '.__FUNCTION__.'() arr:'.implode(',',$arr).PHP_EOL;
 //        echo 'DEBUG: '.__FUNCTION__.'() n:'.$n.PHP_EOL;
 //    } 
  foreach ($arr as $key => $value) {
    if($i++ ==$n){
      // if($debugMode) {
     //        echo 'DEBUG: '.__FUNCTION__.'() get:'.implode(',',$value).PHP_EOL;
   //     } 
      return $value;
    }
  }
  return false;
}

function check_magic_quotes_gpc(){
  global $env_magic_quotes_gpc;
  if($env_magic_quotes_gpc===null){
    if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc() ){
      $env_magic_quotes_gpc = true;
    }else{
      $env_magic_quotes_gpc = false;
    } 
  }
  return $env_magic_quotes_gpc;
}

function safe_json_str_decode($str,$env=null){
  if($env===null){
    if(check_magic_quotes_gpc()){
      return stripslashes($str);
    }
  }
  return $str;  
}

function max_len($x,$lv=0){
  $len=0;
  if($lv) echo 'DEBUG: '.__FUNCTION__.'() lv='.$lv.PHP_EOL;
  if(is_array($x)){
    foreach ($x as $key => $value) {
      if(is_array($value)){
        $len += max_len($value,$lv+1);
      }else{
        $len += strlen($x); 
      }
    }
  }else{
    return strlen($x);
  }
  return $len;
}

// $str1 = '布局 1 介绍 布局，简单来说就是设置元素的大小和位置。 Ext 的布局系统包括组件，布局，容器，容器是一种特殊的组件，可以管理组件的大小和位置。 容器是通过 doLayout 来重新计算布局，并更新 DOM. 2 手工布局是不必要的，框架会为你自动处理。';

// $str2 = '!@#$%^&*()QWERTYUIOPSDFGHJKL!@#$%^&*()QWERTYUIOPSDFGHJKL:ZXCVBNMa!@#$%^&*()ERTYUIODFGHJKLXCVBNM@#$%^&*()RTYUIOPD:ZXCVBNM#!@#!@#$%^&*()QWERTYUIOPSDFGHJKL:ZXCVBNM-!@#$%^&*()ERTYUIODFGHJKLXCVBNM@#$%^&*()RTYUIOPD$%^&*()ERTYUIODFGHJ!@#$%^&*()QWERTYUIOPSDFGHJKL:ZXCVBNM]!@#$%^&*()ERTYUIODFGHJKLXCVBNM@#$%^&*()RTYUIOPDKLXCVBNM@#$%^&*()RTYUIOPDFGHJKLCVBNMFGHJTYU%^&RFGHJ4d56g7h8ui7h8ujirqwerqh8';

// echo '<b>压缩中文比较</b><br><br>';
// compress_comp($str1, 1000); // 压缩1000次 与 解压缩1000次比较

// echo '<b>压缩英文数字比较</b><br><br>';
// compress_comp($str2, 1000); // 压缩1000次 与 解压缩1000次比较

// /* 压缩 */
// function compress_comp($str, $num){

//  $func_compress = array('gzcompress', 'gzencode', 'gzdeflate', 'bzcompress');

//  echo '原文:'.$str.'<br><br>';
//  echo '原文大小:'.strlen($str).'<br><br>';

//  for($i=0,$length=count($func_compress); $i<$length; $i++){

//    $starttime = get_microtime();
//    for($j=0; $j<$num; $j++){
//      switch($func_compress[$i]){
//        case 'gzcompress':
//          $mstr = gzcompress($str, 9); // 解压方法：gzuncompress
//          break;
//        case 'gzencode':
//          $mstr = gzencode($str, 9); // 解压方法：gzdecode php>=5.4
//          break;
//        case 'gzdeflate':
//          $mstr = gzdeflate($str, 9); // 解压方法：gzinflate
//          break;
//        case 'bzcompress':
//          $mstr = bzcompress($str, 9); // 解压方法：bzdecompress
//          break;      
//      }
//    }
//    $endtime = get_microtime();
//    echo $func_compress[$i].' 压缩后大小:'.strlen($mstr).' 耗时:'.round(($endtime-$starttime)*1000,5).'ms<br><br>';
//  }
// }


// /* 获取 microtime */
// function get_microtime(){
//  list($usec, $sec) = explode(' ', microtime(true));
//  return $usec+$sec;
// }


function arr_del($arr1,$key){
    $c = count($arr1);        
    $arrkey = array_search($key, $arr1);
    if ($arrkey !== false)
        array_splice($arr1, $arrkey, 1);
    if($c==count($arr1)){
        echo 'DEBUG: '.__FUNCTION__.'() Error! count:'. $c.'<br/>'.PHP_EOL;
    }
    return $arr1;
}


$lastColorId=null;
function get_color($id=false){
  global $lastColorId;
  $debugMode = get_func_debug_mode(__METHOD__);
  if($debugMode) echo 'DEBUG: '.__METHOD__.'() id='. $id.'<br/>'.PHP_EOL;

  if($id && strlen($id)>3 && stripos($id, '#')===false){
    return $id;
  }

  $arr = explode(',', 'ffffff,dddddd,aaaaaa,888888,666666,444444,000000,ffb7dd,ff88c2,ff44aa,ff0088,c10066,a20055,8c0044,ffcccc,ff8888,ff3333,ff0000,cc0000,aa0000,880000,ffc8b4,ffa488,ff7744,ff5511,e63f00,c63300,a42d00,ffddaa,ffbb66,ffaa33,ff8800,ee7700,cc6600,bb5500,ffee99,ffdd55,ffcc22,ffbb00,ddaa00,aa7700,886600,ffffbb,ffff77,ffff33,ffff00,eeee00,bbbb00,888800,eeffbb,ddff77,ccff33,bbff00,99dd00,88aa00,668800,ccff99,bbff66,99ff33,77ff00,66dd00,55aa00,227700,99ff99,66ff66,33ff33,00ff00,00dd00,00aa00,008800,bbffee,77ffcc,33ffaa,00ff99,00dd77,00aa55,008844,aaffee,77ffee,33ffdd,00ffcc,00ddaa,00aa88,008866,99ffff,66ffff,33ffff,00ffff,00dddd,00aaaa,008888,cceeff,77ddff,33ccff,00bbff,009fcc,0088a8,007799,ccddff,99bbff,5599ff,0066ff,0044bb,003c9d,003377,ccccff,9999ff,5555ff,0000ff,0000cc,0000aa,000088,ccbbff,9f88ff,7744ff,5500ff,4400cc,2200aa,220088,d1bbff,b088ff,9955ff,7700ff,5500dd,4400b3,3a0088,e8ccff,d28eff,b94fff,9900ff,7700bb,66009d,550088,f0bbff,e377ff,d93eff,cc00ff,a500cc,7a0099,660077,ffb3ff,ff77ff,ff3eff,ff00ff,cc00cc,990099,770077');
  $c = count($arr);

  if($id===false){
    if($debugMode) echo 'DEBUG: '.__METHOD__.'() mode 1.<br/>'.PHP_EOL;
    if($lastColorId==null) {
      $n=1;
      $lastColorId=1;
    }else{
      $n = mt_rand(0,$c-1);
      $max = 10;
      while ( $n== $lastColorId && $max>0 ) {
        $n = mt_rand(0,$c-1);
        $max--;
      }
      $lastColorId=$n;
    }   
  }else if(stripos($id, '#')!==false){
    if($debugMode) echo 'DEBUG: '.__METHOD__.'() mode 2.<br/>'.PHP_EOL;
    $tmpArr = explode('#', $id);
    $ccid = $tmpArr[count($tmpArr)-1];    
    if(strlen($ccid)==6) {
      if($debugMode) echo 'DEBUG: '.__METHOD__.'() #2 return='. $ccid.'<br/>'.PHP_EOL;
      return $ccid;
    }else{
      if($debugMode) echo 'DEBUG: '.__METHOD__.'() #2 ccid='. $ccid.'<br/>'.PHP_EOL;
      $n = $ccid;
      $n = fmod(+$id,$c);
    }
  }else if(+$id==intval($id)){ 
    if($debugMode) echo 'DEBUG: '.__METHOD__.'() mode 3.<br/>'.PHP_EOL;
    $n = fmod(+$id,$c);
  }else{
    if($debugMode) echo 'DEBUG: '.__METHOD__.'() mode else:<br/>'.PHP_EOL;
    // $n = fmod($id,$c);
    $n = mt_rand(0,$c-1);  
  }

  if($debugMode) echo 'DEBUG: '.__METHOD__.'() return='. $arr[$n].'<br/>'.PHP_EOL;

  return $arr[$n];
}


?>
<?php
